package com.hand.demo.app.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hand.demo.domain.entity.LmExample;
import com.hand.demo.infra.repository.impl.LmExampleRepositoryImpl;
import org.hzero.boot.imported.app.service.IDoImportService;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 *
 */
@ImportService(templateCode = "TEST-ClIENT-44589")
public class DemoTestClients implements IDoImportService {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private LmExampleRepositoryImpl lmExampleRepository;

    public static final Logger logger = LoggerFactory.getLogger(DemoTestClients.class);

    @Override
    public Boolean doImport(String data) {
        LmExample lmExample;
        try {
            lmExample = objectMapper.readValue(data, LmExample.class);
        } catch (IOException e) {
            logger.info(e.getMessage());
            return false;
        }
        if (lmExample.getName().equals("zhangsan")) {
            lmExampleRepository.insertSelective(lmExample);
        }
        return true;
    }

    @Override
    public void onStart() {
        IDoImportService.super.onStart();
    }

    @Override
    public void onFinish() {
        IDoImportService.super.onFinish();
    }
}
